//
//  RegisterViewController.m
//  ShopProject
//
//  Created by 小焘无量 on 2018/9/27.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "RegisterViewController.h"
#import "OO_WebViewController.h"
#import "PrivacyPolicyViewController.h"

@interface RegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *telPhoneTF;
@property (weak, nonatomic) IBOutlet UITextField *firstPasswordTF;
@property (weak, nonatomic) IBOutlet UITextField *secondPasswordTF;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfig];
    
//    [self dataTest];
}

-(void) UIConfig {
    self.title = @"注册";
    _registerBtn.backgroundColor = MainColor;
}

// 注册
- (IBAction)registerBtnPress:(id)sender {
    
    [self.view endEditing:YES];
    
    if (![self registerDateCheck]) {
        return;
    }
    // 数据库操作
    JQFMDB *dataBase = [JQFMDB shareDatabase];
    OO_UserInfoModel *model = [OO_UserInfoModel new];
    model.userName = _userNameTF.text;
    model.phone = _telPhoneTF.text;
    model.password = _secondPasswordTF.text;
    
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    dic[@"userName"] = _userNameTF.text;
    dic[@"phone"] = _telPhoneTF.text;
    dic[@"password"] = _secondPasswordTF.text;
    
    // 先查询是否注册过
    NSArray *arr = [dataBase jq_lookupTable:USERINFOTABLE dicOrModel:[OO_UserInfoModel class] whereFormat:@"where phone = %@",_telPhoneTF.text];
    [MBProgressHUD showActivityMessageInWindow:@"Loading"];
    if(arr.count == 0 || arr == nil) {
        //  未注册
        dic[@"type"] = @"1";
        if ([dataBase jq_insertTable:USERINFOTABLE dicOrModel:model]) {
            [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,REGISTER_URL] parameters:dic success:^(id responseObject) {
                NSDictionary *dict = (NSDictionary *)responseObject;
                NSLog(@"%@",dict);
                [MBProgressHUD hideHUD];
                [MBProgressHUD showTipMessageInView:@"注册成功"];
            } failure:^(NSError *error) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showTipMessageInView:@"网络请求失败"];
            }];
        }else {
            // 写入数据库失败
            [MBProgressHUD hideHUD];
            [MBProgressHUD showTipMessageInView:@"未知错误"];
        }
    }else {
        // 已注册
        dic[@"type"] = @"0";
        
        [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,REGISTER_URL] parameters:dic success:^(id responseObject) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showTipMessageInView:@"该手机号已注册"];
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showTipMessageInView:@"网络请求失败"];
        }];
    }
    

}
- (IBAction)privacyPolicyBtnPress:(UIButton *)sender {
    PrivacyPolicyViewController *vc = [[PrivacyPolicyViewController alloc] init];
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext|UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

// 返回注册
- (IBAction)backToLoginBtnPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

// 数据检测
-(BOOL)registerDateCheck {
    if ([CommonAPI StringIsNull:_userNameTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入用户名"];
        return NO;
    }
    if ([CommonAPI StringIsNull:_telPhoneTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入手机号"];
        return NO;
    }
    if (![CommonAPI telPhoneCheck:_telPhoneTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入正确的手机号"];
        return NO;
    }
    if ([CommonAPI StringIsNull:_firstPasswordTF.text]) {
        [MBProgressHUD showTipMessageInView:@"请输入密码"];
        return NO;
    }
    if (![CommonAPI passwordCheck:_firstPasswordTF.text]) {
        [MBProgressHUD showTipMessageInView:@"密码请保持在6-16位"];
        return NO;
    }
    if (![_firstPasswordTF.text isEqualToString:_secondPasswordTF.text]) {
        [MBProgressHUD showTipMessageInView:@"两次密码请保持一致"];
        return NO;
    }
    return YES;
}

// 单元测试数据
-(void)dataTest {
    _userNameTF.text = @"测试1";
    _telPhoneTF.text = @"13219011308";
    _firstPasswordTF.text = @"123456";
    _secondPasswordTF.text = @"123456";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
